﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LifeFlow.Servicer
{
    public class ServiceVersion
    {
        public ServiceVersion(int? major, int? minor)
        {
            Major = major;
            Minor = minor;
        }

        public ServiceVersion(string version)
        {
            var parts = version.Split('.');

            int major = -1;
            int minor = -1;
            int.TryParse(parts[0].Trim(), out major);
            if (parts.Length >= 2)
                int.TryParse(parts[1].Trim(), out minor);

            if (major >= 0)
                Major = major;
            if (minor >= 0)
                Minor = minor;
        }

        public int? Major { get; set; }
        public int? Minor { get; set; }

        

        public static bool HasAccess(ServiceVersion requestVersion, ServiceVersion min, ServiceVersion max)
        {
            if (min != null && requestVersion.Major < min.Major)
                return false;
            if (min != null && requestVersion.Major == min.Major &&
                requestVersion.Minor < min.Minor)
                return false;

            if (max != null && requestVersion.Major > max.Major)
                return false;
            if (max != null && requestVersion.Major == max.Major &&
                requestVersion.Minor > max.Minor)
                return false;

            return true;
        }
    }
}
